//当前路径
var rootPath = getRootPath();
// 是否为开发环境
var isDev = rootPath.indexOf("weixin-dev") > -1;
var weChatPath = isDev ? "http://weixin-dev6.szsanshan.com" : "";
// cookie
var COOKIE_OPENID = "seeTools-openid";
// memberId
var COOKIE_MEMBER_ID = "seeTools-member-id";


/**
 * 功能描述：获取系统根路径
 */
function getRootPath() {
    // 获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp
    var curWwwPath = window.document.location.href;
    // 获取主机地址之后的目录，如： uimcardprj/share/meun.jsp
    var pathName = window.document.location.pathname;
    var pos = curWwwPath.indexOf(pathName);
    // 获取主机地址，如： http://localhost:8083
    var localhostPaht = curWwwPath.substring(0, pos);
    // 获取带"/"的项目名，如：/uimcardprj
    var projectName = pathName.substring(0, pathName.substring(1).indexOf('/') + 1);
    if (projectName == "/wechat") {
        return localhostPaht + projectName;
    } else {
        return localhostPaht;
    }
}

/**
 * 判断cookie中是否有openid，没有则需要重新授权
 */
$(function () {
    var openid = getCookie(COOKIE_OPENID);
    if (!openid) {
        window.location.href = getRootPath() + '/authorize?returnUrl=' + window.location.href;
    }
});

/**
 * 读取页面cookie
 * @param name
 * @returns {*}
 */
function getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg))
        return unescape(arr[2]);
    else
        return null;
}

/**
 * 设置cookie
 * @param c_name
 * @param value
 * @param expiredays
 */
function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}

/**
 * 获取URL参数值
 * @param key
 * @returns {*}
 */
function getUrlParam(key) {
    var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

/**
 * 获取用户基础信息
 */
function oAuth2UserInfo() {
    window.location.href = getRootPath() + '/oauth2Code?returnUrl=' + window.location.href;
}

// 粉丝工具类
const MemberUtils = {
    getUserInfo: function () {
        return $.ajax({
            method: 'GET',
            url: rootPath + "/wx/user/info/" + getCookie(COOKIE_OPENID)
        });
    },
    // 获取会员信息
    getMemberInfo: function (type) {
        type = type || 'nocache';
        return $.ajax({
            method: 'GET',
            url: rootPath + "/wx/member/info/" + getCookie(COOKIE_OPENID) + "/" + type
        });
    },
    getCouponInfo: function (memberId) {
        return $.ajax({
            method: 'GET',
            url: rootPath + "/wx/coupon/info/" + memberId
        });
    },
    // 获取会员券类型数据
    getCouponTypeNumber: function (memberId) {
        return $.ajax({
            method: 'GET',
            url: rootPath + "/wx/coupon/type/number/" + memberId
        });
    },
    // 获取优惠券券包
    getCouponPacket: function (params) {
        return $.ajax({
            method: 'POST',
            contentType: "application/json",
            url: rootPath + "/wx/coupon/packet",
            data: JSON.stringify(params)
        });
    },
    // 获取可用优惠券
    getCouponUsable: function (params) {
        return $.ajax({
            method: 'POST',
            contentType: "application/json",
            url: rootPath + "/wx/coupon/usable",
            data: JSON.stringify(params)
        });
    },
    getSignInData: function (memberId) {
        return $.ajax({
            method: 'GET',
            url: rootPath + "/wx/member/sign/" + memberId
        });
    },
    signIn: function (params) {
        return $.ajax({
            method: 'POST',
            contentType: "application/json",
            url: rootPath + "/wx/member/sign/in",
            data: JSON.stringify(params)
        });
    },
    getPointRecord: function (params) {
        return $.ajax({
            method: 'POST',
            contentType: "application/json",
            url: rootPath + "/wx/member/point",
            data: JSON.stringify(params)
        });
    }
};

// 便利店工具类
const StoreUtils = {
    getStoreMall: function () {
        return $.ajax({
            method: 'GET',
            url: rootPath + "/wx/store/getStoreMall",
        });
    },
    getStoreSale: function (memberId) {
        return $.ajax({
            method: 'GET',
            url: rootPath + "/wx/store/getStoreSale/" + memberId,
        });
    }
};

// 储值卡工具类
const CardUtils = {
    getCardList: function (memberId) {
        return $.ajax({
            method: 'GET',
            url: rootPath + "/wx/card/getCardList/" + memberId,
        });
    },
    openCard: function (memberId) {
        return $.ajax({
            method: 'GET',
            url: rootPath + "/wx/card/openCard/" + memberId,
        });
    },
    getCardInfoByNo: function (cardNo) {
        return $.ajax({
            method: 'GET',
            url: rootPath + '/wx/card/getCardInfoByNo/' + cardNo
        })
    },
    getCardRechargeRecord: function (params) {
        return $.ajax({
            method: 'POST',
            contentType: "application/json",
            url: rootPath + '/wx/card/getCardRechargeRecord',
            data: JSON.stringify(params)
        })
    }
}

// 外部API接口工具类
const ApiUtil = {
    // 获取每日一笑段子
    findJokes: function () {
        return $.ajax({
            method: 'GET',
            url: rootPath + "/wx/api/findJokes/",
        });
    }
}

const WXUtils = {
    goHome: function () {
        window.location.href = getRootPath() + "/pages/member/center.html"
    },
    submitPayOrder: function (params) {
        return $.ajax({
            type: 'POST',
            url: rootPath + '/wx/pay/submit',
            data: JSON.stringify(params),
            contentType: "application/json"
        })
    }
};
